package com.zzyl.nursing.service.impl;

import java.util.Collection;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.dto.AlertDataDto;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 报警数据Service业务层处理
 * 
 * @author laoYe
 * @date 2025-06-05
 */
@Service
@Slf4j
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {

        // 设置处理人id
        alertData.setProcessorId(alertData.getUserId());
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 处理报警数据
     *
     * @param alertData
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void handleAlertData(AlertData alertData) {

        //查询报警数据
        AlertData alertDataById = alertDataMapper.selectAlertDataById(alertData.getId());
        //判断报警数据是否为空
        if(alertDataById==null){
            log.info("报警数据不存在{}",alertData.getId());
        }
        //查询处理人
        AlertData alertData1 = alertDataMapper.selectName(alertDataById);
        //拷贝数据
        BeanUtil.copyProperties(alertData,alertDataById);
        //将 alertDataById 的更新者设置为 alertData1 的用户 ID。
        alertDataById.setUpdateBy(alertData1.getUserId()+"");
        //更新处理结果
        alertDataById.setProcessingResult(alertData.getProcessingResult());
        //更新处理时间
        alertDataById.setProcessingTime(alertData.getProcessingTime());
        //更新处理人
        alertDataById.setProcessorId(alertData1.getProcessorId());
        //更新处理人名称
        alertDataById.setProcessorName(alertData1.getProcessorName());
        //更新物联网设备id
        alertDataById.setIotId(alertDataById.getIotId());
        //更新状态
        alertDataById.setStatus(1);
        alertDataMapper.updateAlertData(alertDataById);
    }

    @Override
    public TableDataInfo selectAllAlert(AlertDataDto dto) {
        Page<AlertData> page = new Page<>(dto.getPageNum(), dto.getPageSize());

        lambdaQuery().eq(ObjectUtil.isNotEmpty(dto.getStatus()), AlertData::getStatus, dto.getStatus())
                .like(ObjectUtil.isNotEmpty(dto.getDeviceName()), AlertData::getDeviceName, dto.getDeviceName())
                .between(ObjectUtil.isNotEmpty(dto.getStartTime()) && ObjectUtil.isNotEmpty(dto.getEndTime()), AlertData::getCreateTime, dto.getStartTime(), dto.getEndTime())
                .page(page);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setTotal(page.getTotal());

        return tableDataInfo;
    }
}
